package com.aurora.spider.houyuantuan;

import com.aurora.event.HouyuantuanEvent;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.StartSchedulerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by jackie on 18/1/15.
 */
@PipelineName("personDealLine")
@Component
@Slf4j
public class PersonDealLine implements Pipeline<Person>, ApplicationContextAware {

    public static final ConcurrentHashMap cach = new ConcurrentHashMap();

    private static ApplicationContext applicationContext;

    @Override
    public void process(Person person) {
        Object o = cach.get(person.getId()); //防止重复扫描一个人
        if (o == null) {
            cach.put(person.getId(), "1");
            //保存人
            applicationContext.publishEvent(new HouyuantuanEvent(person));
            //找其他关系
            Long starId = person.getId();
            HttpRequest currRequest = person.getRequest();
            String url = "https://www.houyuantuan.com/mingxing/" + starId + "/guanxi.html";
            StartSchedulerContext.into(currRequest.subRequest(url));
        }
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
